package com.pw.study.gmalllogger.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pw.study.common.constants.TopicConstant;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Log4j
public class LogController {
    @Autowired
    private KafkaTemplate<String, String> producer;

    @GetMapping("/log")
    public Object handle(String param) {
        log.info("param:" + param);
        JSONObject obj = JSON.parseObject(param);
        if (obj.containsKey("start")) {
            System.out.println("============================");
            producer.send(TopicConstant.STARTUP_LOG, param);
        }
        if (obj.containsKey("page")) {
            producer.send(TopicConstant.PAGE_LOG, param);
        }
        if (obj.containsKey("actions")) {
            producer.send(TopicConstant.ACTIONS_LOG, param);
        }
        if (obj.containsKey("displays")) {
            producer.send(TopicConstant.DISPLAY_LOG, param);
        }
        if (obj.containsKey("err")) {
            producer.send(TopicConstant.ERROR_LOG, param);
        }


        return "ok";
    }

}
